const values_default = {}

const domain_default = {}

const fields_default = {
  payment_type: null,
  partner_type: null,
  state: null,
  name: null,
  company_id: null,
  partner_id: null,
  amount: null,
  payment_date: null,
  communication: null,
  journal_id: null,
  payment_method_id: null,
  reconciled_invoice_ids: { name: null },
  has_invoices: null,
  reconciled_invoices_count: null
  // move_line_ids: { name: null }
}

const filterItems = [
  { type: 'input', name: 'name___like', placeholder: '付款号' },
  { type: 'date', name: 'payment_date', placeholder: '日期' },
  { type: 'select', name: 'partner_id', placeholder: '业务伙伴' }
]

const columns = [
  { name: 'payment_type__name', type: 'char', label: '收或付' },
  { name: 'partner_type__name', type: 'char', label: '客户或供应商' },
  { name: 'partner_id__name', type: 'char', label: '业务伙伴' },
  { name: 'company_id__name', type: 'char', label: '公司' },
  { name: 'state__name', type: 'char', label: '状态' },
  { name: 'name', type: 'char', label: '付款单号' },
  { name: 'payment_date', type: 'char', label: '日期' },
  { name: 'journal_id__name', type: 'char', label: '付款方式' },
  { name: 'payment_method_id__name', type: 'char', label: '付款方法' },
  { name: 'amount', type: 'char', label: '金额' },
  { name: 'communication', type: 'char', label: '备注' }
]

const formItems = [
  //  { type: 'input', name: 'name', label: '摘要' },
  {
    type: 'select',
    name: 'partner_id',
    label: '客户',
    placeholder: '客户'
  },

  { type: 'date', name: 'payment_date', label: '日期' },
  {
    type: 'select',
    name: 'journal_id',
    onChange: ({ self, value, formItem, temp }) => {
      const options = formItem.options_for_select_init
      const journal = options.filter(it => it.id === value)[0]

      const { payment_type } = self.configs.values_default

      let payment_method_ids = []

      if (payment_type === 'inbound') {
        payment_method_ids = journal.inbound_payment_method_ids || []
      } else {
        payment_method_ids = journal.outbound_payment_method_ids || []
      }

      let payment_method_id = temp.payment_method_id

      if (!payment_method_id) {
        payment_method_id = payment_method_ids[0]
      } else {
        if (!payment_method_ids.includes(payment_method_id)) {
          payment_method_id = payment_method_ids[0]
        }
      }

      return { payment_method_id }
    },
    default: params => {
      console.log('xxxxxx, default,', params)
      const { formItem } = params

      const options = formItem.options_for_select_init
      const journal = options[0]
      return journal.id
    },
    options: {
      fields: {
        inbound_payment_method_ids: { name: null },
        outbound_payment_method_ids: { name: null }
      }
    },
    label: '付款方式',
    placeholder: '付款方式'
  },
  {
    type: 'select',
    name: 'payment_method_id',
    label: '付款方法',
    placeholder: '付款方法',
    options: {
      fields: {},
      domain: {}
    },
    options_for_select: params => {
      // console.log('xxxxxx, 1', params)
      const { self, temp, items } = params
      const journals = items.journal_id.options_for_select_init.filter(
        it => it.id === temp.journal_id
      )

      const journal = journals[0] || {}
      const { payment_type } = self.configs.values_default
      // console.log('xxxxxx,2 ', journal)

      if (payment_type === 'inbound') {
        return journal.inbound_payment_method_ids__objects || []
      } else {
        return journal.outbound_payment_method_ids__objects || []
      }
    }
  },
  { type: 'number', name: 'amount', label: '金额' },
  { type: 'text', name: 'communication', label: '备注' }
]

const workflow = {
  states: {
    draft: '草稿',
    posted: '已过账',
    reconciled: '已核销',
    cancel: '已取消'
  },

  actions: {
    update: { value: '', name: '修改', type: 'primary' },
    // to_reconcile: { value: '', name: '核销', type: 'primary' },

    draft: {
      name: '重置为草稿',
      method: 'action_draft',
      last_in: ['posted', 'cancel'],
      odoo_type: 'object',
      type: 'primary'
    },
    posted: {
      name: '确认',
      method: 'post',
      last_in: ['draft'],
      odoo_type: 'object',
      type: 'primary'
    },
    cancel: {
      name: '取消',
      last_in: ['draft'],
      method: 'cancel',
      odoo_type: 'object'
    }
  },

  transfers: {
    draft: {
      actions: ['update', 'posted', 'cancel'],
      states: ['draft', 'posted']
    },
    posted: {
      actions: [],
      states: ['draft', 'posted']
    },
    cancel: {
      actions: ['draft'],
      states: ['draft', 'cancel']
    }
  }
}

const Model = {
  configs: {
    workflow,
    values_default,
    domain_default,
    fields_default,
    filterItems,
    columns,
    formItems
  },

  extend: BaseClass => {
    class ModelClass extends BaseClass {
      global_domain(domain = {}) {
        return super.global_domain(domain)
      }
    }
    return ModelClass
  }
}

export default Model
